<template>
  <div class="article-list">
    <div class="article-card"
         v-for="item in formattedList"
         :key="item.id"
         v-motion
         :initial="{ opacity: 0, y: 20 }"
         :enter="{ opacity: 1, y: 0 }">
      <!-- 文章封面 -->
      <div class="card-cover">
        <img :src="item.coverImage"
             loading="lazy"
             alt=""
             @error="handleImageError" />
      </div>

      <!-- 文章内容 -->
      <div class="card-content">
        <div class="meta">
          <time class="date">{{ item.formattedDate }}</time>
          <span class="category" @click="handleCheckedCategory(item.category,item.categorySlug)">{{ item.categoryName }}</span>
        </div>

        <router-link :to="`/detail/${item.slug}?id=${item.id}`"
                     class="title">
          {{ item.title }}
        </router-link>

        <p class="summary">{{ item.summary }}</p>

        <div class="footer">
          <div class="stats">
            <span class="views">
              <PhEye :size="18" weight="fill" class="icon-eye" color="#6b7280" />
              {{ item.views }} 阅读
              <PhChatDots :size="18" weight="fill" class="icon-dialog" color="#6b7280" />
              {{ item.commentsCount }} 评论
            </span>
          </div>
          <router-link class="read-more" :to="`/detail/${item.slug}?id=${item.id}`">
            阅读全文
            <PhArrowRight class="icon-arrow" :size="16" color="#0d0d0d" weight="light" />
          </router-link>
        </div>
      </div>

      <!-- 置顶标记 -->
      <div class="sticky-badge" v-if="item.isSticky">
        <span>置顶</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import type { PropType } from 'vue';
import type { Article } from '@/api/article/types.ts';
import png from '@/assets/images/fallback-image.jpg';
import { useRouter } from 'vue-router';
import { PhArrowRight, PhChatDots, PhEye } from '@phosphor-icons/vue';

const router = useRouter();

const { list } = defineProps({
  list: {
    type: Array as PropType<Article[]>,
    required: true,
  },
});

const formatDate = (date: string) => {
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
};

const formattedList = computed(() => {
  return list.map(item => ({
    ...item,
    formattedDate: formatDate(item.publishTime)
  }));
});

const handleImageError = (e: Event) => {
  const target = e.target as HTMLImageElement;
  target.src = png;
};

// 处理点击分类
const handleCheckedCategory = (id:string,slug:string) => {
  router.push(`/category/${slug}?id=${id}`);
};
</script>
<style scoped lang="less">
.article-list {
  width: 100%;
  margin: 0 auto;
}

.article-card {
  position: relative;
  display: flex;
  width: 100%;
  height: 232px;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 24px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);

  &:hover {
    transform: translateY(-4px);

    .card-cover img {
      transform: scale(1.08) rotate(-1deg);
      filter: saturate(1.2);
    }

    .read-more {
      color: #3b82f6;

      .icon-arrow {
        transform: translateX(4px);
      }
    }
  }

  // 文章封面
  .card-cover {
    position: relative;
    width: 300px;
    height: 100%;
    overflow: hidden;
    clip-path: polygon(0 0, 100% 0, 94% 100%, 0% 100%);

    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(
        to right,
        rgba(0, 0, 0, 0) 0%,
        rgba(0, 0, 0, 0.2) 100%
      );
      z-index: 1;
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: all 0.6s cubic-bezier(0.4, 0, 0.2, 1);
      filter: saturate(1.1);
    }
  }

  // 文章内容
  .card-content {
    position: relative;
    flex: 1;
    height: 100%;
    padding: 28px 36px;
    display: flex;
    flex-direction: column;
    background: linear-gradient(
      to right,
      rgba(255, 255, 255, 0.95) 0%,
      rgba(255, 255, 255, 1) 100%
    );

    .meta {
      display: flex;
      align-items: center;
      gap: 16px;
      margin-bottom: 16px;

      .date {
        color: #6b7280;
        font-size: 14px;
      }

      .category {
        background-color: #f43f5e;
        padding: 4px 12px;
        border-radius: 12px;
        color: white;
        font-size: 0.9em;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-3px);
        }
      }
    }

    .title {
      font-size: 21px;
      font-weight: 600;
      line-height: 1.4;
      letter-spacing: 0.3px;
      color: #1f2937;
      margin-bottom: 14px;
      text-decoration: none;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      overflow: hidden;
      transition: color 0.3s;

      &:hover {
        color: #3b82f6;
      }
    }

    .summary {
      position: relative;
      flex: 1;
      font-size: 14.5px;
      line-height: 1.8;
      color: #4b5563;
      margin-bottom: 24px;
      padding: 0 28px 0 24px;
      max-height: calc(1.8em * 2);
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      text-overflow: ellipsis;
      text-indent: 1.5em;
      font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
      letter-spacing: 0.5px;

      // 确保文本不会溢出容器
      > * {
        text-overflow: ellipsis;
        overflow: hidden;
      }

      &::before {
        content: '"';
        position: absolute;
        left: 0;
        top: 0;
        font-size: 24px;
        color: #9ca3af;
        opacity: 0.6;
        text-indent: 0;
        line-height: 1;
        z-index: 1;
      }

      &::after {
        content: '"';
        position: absolute;
        right: 0;
        bottom: 0;
        font-size: 24px;
        color: #9ca3af;
        opacity: 0.6;
        text-indent: 0;
        line-height: 1;
        z-index: 1;
      }
    }

    .footer {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .stats {
        color: #6b7280;
        font-size: 14px;

        .views {
          display: flex;
          align-items: center;
          gap: 4px;

          .icon-eye {
            margin-right: 4px;
          }

          .icon-dialog {
            margin: 0 4px;
          }
        }
      }

      .read-more {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 14px;
        font-weight: 500;
        color: #4b5563;
        transition: all 0.3s;
        cursor: pointer;

        .icon-arrow {
          transition: transform 0.3s;
        }
      }
    }
  }

  // 置顶标记
  .sticky-badge {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 10;

    span {
      display: inline-block;
      padding: 6px 16px;
      background: linear-gradient(135deg, #3b82f6, #60a5fa);
      color: #fff;
      font-size: 13px;
      font-weight: 500;
      border-radius: 20px;
      box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
    }
  }
}

// 响应式设计
@media (max-width: 992px) {
  .article-list {
    width: 100%;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .article-card {
    height: auto;
    flex-direction: column;

    .card-cover {
      width: 100%;
      height: 200px;
      clip-path: none;

      &::after {
        background: linear-gradient(
          to bottom,
          rgba(0, 0, 0, 0) 0%,
          rgba(0, 0, 0, 0.3) 100%
        );
      }
    }

    .card-content {
      padding: 24px;
      background: #fff;

      .title {
        font-size: 18px;
        -webkit-line-clamp: 1;
      }

      .summary {
        font-size: 14px;
        margin-bottom: 16px;
        padding: 0 28px 0 24px; // 保持与PC端相同的内边距
        text-indent: 1.5em; // 保持与PC端相同的缩进
        -webkit-line-clamp: 2; // 保持2行显示

        &::before {
          left: 0;
          top: 0;
          font-size: 20px; // 移动端稍微调小引号大小
        }

        &::after {
          right: 0;
          bottom: 0;
          font-size: 20px; // 移动端稍微调小引号大小
        }
      }
    }
  }
}

@media (max-width: 576px) {
  .article-card {
    .card-cover {
      height: 180px;
    }

    .card-content {
      .summary {
        padding: 0 24px 0 20px; // 在更小屏幕上稍微减小内边距
      }
    }
  }
}
</style>
